<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
class VoteController extends HomeBaseController {
	
	
	protected $db;
	function _initialize(){
		parent::_initialize();

		$this->vote = D("Vote");
		$this->question = D("VoteQuestion");
		$this->answer = D("VoteAnswer");
		$this->log = D("VoteLog");
	}



//	function myvote(){
//
//
//
//		$this->assign ( "seo_title",  "我的问卷/投票" );
//
//
//		$map["uid"]=$this->u["id"];
//
//		$count = D("ViewsVote")->where($map)->group("vote_id")->count ();
//
//		$page = $this->page ( $count, 10 );
//		$list=D("ViewsVote")->field('uid,title,datetime,id')->where($map)->group("vote_id")->limit ( $page->firstRow . ',' . $page->listRows )->order("id desc")->select();
//
//
//		foreach($list as $k=>$v){
//
//			if(date("Y-m-d H:i:s")>$v["endtime"]){
//
//				$list[$k][guoqi]=0;
//
//
//			}
//			else{
//				$list[$k][guoqi]=1;
//
//			}
//
//		}
//		$this->assign ( "Page", $page->show () );
//
//		$this->assign("_list",$list);
//
//
//		$this->display ();
//
//
//	}

	/**
	 *投票
     */
//	function index(){
//
//
//
//
//		$this->assign ( "seo_title",  "调查问卷" );
//
//
//		$map["status"]=1;
//
//		$count = $this->vote->where($map)->count ();
//		$page = $this->page ( $count, 10 );
//		$list=$this->vote->where($map)->limit ( $page->firstRow . ',' . $page->listRows )->order("id desc")->select();
//
//
//
//
//
//		foreach($list as $k=>$v){
//
//			if(date("Y-m-d H:i:s")>$v["endtime"]){
//
//				$list[$k][guoqi]=0;
//
//
//			}
//			else{
//				$list[$k][guoqi]=1;
//
//			}
//
//		}
//		$this->assign ( "Page", $page->show () );
//
//		$this->assign("_list",$list);
//		$this->display ();
//
//
//
//
//	}


	function result(){

		$id=I("get.id");


		$info=$this->vote->find($id);



		$order="listorder desc, id asc";
		$question=$this->question->where(array("vote_id"=>$id))->order($order)->select();



		//获取本人的提交的结果
		$map["vote_id"]=$id;
		$uid=$this->u['id'];;




		foreach($question as $k=>$v){




			if($v['type']=='select'|| $v['type']=='selects'){


				$answers=$this->answer->order($order)->where(array("question_id"=>$v['id']))->select();


				foreach($answers as $kk=>$vv){

					$mydaan=$this->log->where(array("vote_id"=>$id,"uid"=>$uid,"value"=>$vv["id"]))->getField("value",true);



					if($mydaan){
						$answers[$kk]["select"]="checked";
					}




					if($info['result']){



						$num=$this->log->where(array("vote_id"=>$id,"value"=>$vv["id"]))->count();

						$answers[$kk]["num"]=$num;


					}



				}


				$question[$k][answers]=$answers;



			}else{





				$mydaan=$this->log->where(array("vote_id"=>$id,"uid"=>$uid,"question_id"=>$v["id"]))->getField("value");





				$question[$k]["zhi"]=$mydaan;



			}








		}





		$this->assign ( "seo_title",  $info["title"] );



		$this->assign("_data",$info);
		$this->assign("_list",$question);
		$this->display();


	}


	function toupiao(){






		$vote_id=I("post.vote_id");


		$map["vote_id"]=$vote_id;
		$map["uid"]=$this->u['id'];;


		$c=$this->log->where($map)->count();

		if($c){

			$this->error("您已经从提交过!");
		}

		$vote=$this->vote->where(array("id"=>$vote_id))->find();


		if(date("Y-m-d H:i:s")>$vote["endtime"]){

			$this->error("该问卷调查已经过期!");


		}



		$question=$this->question->where(array("vote_id"=>$vote_id))->select();


		foreach($question as $v){


			if($v['type']=="selects"){

				$vs=$_POST[$v['id']];

				foreach($vs as $k=>$vv){


					$data=array(
						"uid"=>$this->u['id'],
						"vote_id"=>$vote_id,
						"datetime"=>time(),
						"value"=>$vv,
						"question_id"=>$v['id'],

					);
					$this->log->add($data);

				}
			}else{
				$data=array(
					"uid"=>$this->u['id'],
					"vote_id"=>$vote_id,
					"datetime"=>time(),
					"value"=>$_POST[$v['id']],
					"question_id"=>$v['id'],

				);
//				dump($data);
				$this->log->add($data);
			}







		}

		$u_where["id"]=$this->u["id"];
		D("Users")->where($u_where)->save(array("vip"=>1));




		$this->success("信息提交完毕，请耐心等待页面跳转",U('User/index'));



	}


	/**
	 *票据
     */
	function  show(){

		$id=I("get.id");


		$info=$this->vote->find($id);


		$order="listorder desc, id asc";
		$question=$this->question->where(array("vote_id"=>$id))->order($order)->select();

		foreach($question as $k=>$v){

			$question[$k]['required']=$v['required']?"required":"";

			if($v['type']=='select'|| $v['type']=='selects'){

				$question[$k][answers]=$this->answer->order($order)->where(array("question_id"=>$v['id']))->select();


			}

		}



		$map["vote_id"]=$id;
		$map["uid"]=$this->u['id'];;


		$c=$this->log->where($map)->count();

		if($c) {

			$this->redirect("result",array('id'=>$id));

			$this->assign("status","yitou");
		}
		$vote=$this->vote->where(array("id"=>$id))->find();


		if(date("Y-m-d H:i:s")>$vote["endtime"]){
			$this->redirect("result",array('id'=>$id));
			$this->assign("status","guoqi");

		}

		$this->assign ( "seo_title",  $info["title"] );



		$this->assign("_data",$info);
		$this->assign("_list",$question);
		$this->display();
	}



	

}